package com.ssy.lingxi.report.serviceimpl;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.constant.OrderMemberTypeEnum;
import com.ssy.lingxi.report.api.vo.request.AddOrderVO;
import com.ssy.lingxi.report.entity.report.OrderAddRecordDO;
import com.ssy.lingxi.report.repository.report.OrderAddRecordRepository;
import com.ssy.lingxi.report.service.IOrderAddRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单新增记录接口实现
 * @author 伍将
 * @date 2021/2/20
 * @version 2.0.0
 */
@Service
public class OrderAddRecordServiceImpl implements IOrderAddRecordService {

    @Resource
    private OrderAddRecordRepository repository;

    private List<OrderAddRecordDO> buildOrderAddRecordDOList(AddOrderVO addOrderVO){
        //供应商订单
        List<OrderAddRecordDO> recordDOS=new ArrayList<>();
        OrderAddRecordDO entity = new OrderAddRecordDO();
        entity.setMemberId(addOrderVO.getMemberId());
        entity.setRoleId(addOrderVO.getRoleId());
        entity.setAmount(addOrderVO.getAmount());
        entity.setDateTime(LocalDateTime.now());
        entity.setMemberType(OrderMemberTypeEnum.VENDOR.getCode());
        recordDOS.add(entity);
        //采购商订单
        OrderAddRecordDO buyerOrder = new OrderAddRecordDO();
        buyerOrder.setMemberId(addOrderVO.getBuyerMemberId());
        buyerOrder.setRoleId(addOrderVO.getBuyerRoleId());
        buyerOrder.setAmount(addOrderVO.getAmount());
        buyerOrder.setDateTime(LocalDateTime.now());
        buyerOrder.setMemberType(OrderMemberTypeEnum.BUYER.getCode());
        recordDOS.add(buyerOrder);
        return recordDOS;
    }

    /**
     * 新增订单记录
     * @author 伍将
     * @date 2021/2/20
     * @param addOrderVO: 新增参数
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> add(AddOrderVO addOrderVO) {
        repository.saveAll(buildOrderAddRecordDOList(addOrderVO));
        return Wrapper.success();
    }


    /**
     * 批量新增订单记录
     * @author 伍将
     * @date 2021/2/22
     * @param addOrderVOList: 新增参数
     **/
    @Override
    public void addList(List<AddOrderVO> addOrderVOList) {
        List<OrderAddRecordDO> entityList = addOrderVOList.stream().flatMap(r ->
            buildOrderAddRecordDOList(r).stream()
        ).collect(Collectors.toList());
        repository.saveAll(entityList);
    }
}
